package com.ruoyi.common.utils;

import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @创建者：zhouwei
 * @创建时间：2022/9/15
 * @描述：
 */
public class ImageCropUtils {
	/**
	 * @param fromFileStr 要裁剪的图片路径
	 * @param saveToFileStr 保存的图片地址
	 * @param suffix 图片后缀
	 * @param width 裁剪后宽度
	 * @param height 裁剪后高度
	 *
	 * @return 成功或失败
	 * */
	public  static boolean createThumbnail( String fromFileStr, String saveToFileStr, String suffix, int width, int height ) throws Exception {
		// fileExtNmae是图片的格式 gif JPG 或png
		// String fileExtNmae="";

		File F = new File(fromFileStr);

		if (!F.isFile()){
			return false;
		}

		File ThF = new File(saveToFileStr);
		BufferedImage buffer = ImageIO.read(F);
		/*
		 * 核心算法，计算图片的压缩比
		 */
		int w= buffer.getWidth();
		int h=buffer.getHeight();
		double ratiox = 1.0d;
		double ratioy = 1.0d;

		ratiox= w * ratiox / width;
		ratioy= h * ratioy / height;

		if( ratiox >= 1){
			if(ratioy < 1){
				ratiox = height * 1.0 / h;
			}else{
				if(ratiox > ratioy){
					ratiox = height * 1.0 / h;
				}else{
					ratiox = width * 1.0 / w;
				}
			}
		}else{
			if(ratioy < 1){
				if(ratiox > ratioy){
					ratiox = height * 1.0 / h;
				}else{
					ratiox = width * 1.0 / w;
				}
			}else{
				ratiox = width * 1.0 / w;
			}
		}
		/*
		 * 对于图片的放大或缩小倍数计算完成，ratiox大于1，则表示放大，否则表示缩小
		 */
		AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratiox, ratiox), null);
		buffer = op.filter(buffer, null);
		//从放大的图像中心截图
		buffer = buffer.getSubimage((buffer.getWidth() - width) / 2, (buffer.getHeight() - height) / 2, width, height);
		//写入文件
		ImageIO.write(buffer, suffix, ThF);

		return true;
	}

	/**
	 * @param fromFileInputStream 要裁剪的图片
	 * @param saveToFileStr 保存的图片地址
	 * @param suffix 图片后缀
	 * @param width 裁剪后宽度
	 * @param height 裁剪后高度
	 *
	 * @return 成功或失败
	 * */
	public  static boolean createThumbnail(InputStream fromFileInputStream, String saveToFileStr, String suffix, int width, int height ) throws Exception{
		// fileExtNmae是图片的格式 gif JPG 或png
		// String fileExtNmae="";
		File ThF = new File(saveToFileStr);
		BufferedImage buffer = ImageIO.read(fromFileInputStream);
		/*
		 * 核心算法，计算图片的压缩比
		 */
		int w= buffer.getWidth();
		int h=buffer.getHeight();
		double ratiox = 1.0d;
		double ratioy = 1.0d;

		ratiox= w * ratiox / width;
		ratioy= h * ratioy / height;

		if( ratiox >= 1){
			if(ratioy < 1){
				ratiox = height * 1.0 / h;
			}else{
				if(ratiox > ratioy){
					ratiox = height * 1.0 / h;
				}else{
					ratiox = width * 1.0 / w;
				}
			}
		}else{
			if(ratioy < 1){
				if(ratiox > ratioy){
					ratiox = height * 1.0 / h;
				}else{
					ratiox = width * 1.0 / w;
				}
			}else{
				ratiox = width * 1.0 / w;
			}
		}
		/*
		 * 对于图片的放大或缩小倍数计算完成，ratiox大于1，则表示放大，否则表示缩小
		 */
		AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratiox, ratiox), null);
		buffer = op.filter(buffer, null);
		//从放大的图像中心截图
		buffer = buffer.getSubimage((buffer.getWidth() - width) / 2, (buffer.getHeight() - height) / 2, width, height);
		try {
			ImageIO.write(buffer, suffix, ThF);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return true;
	}




	private static void listDir(File dir,int level) throws IOException {
		String dirString = dir.toString();
		File[] files = dir.listFiles();
		level++;
		for(File f:files){
			if(f.isDirectory()){
				listDir(f,level);
			}
			else {
				BufferedImage bufferedimage=ImageIO.read(f);
				int width = bufferedimage.getWidth();
				int height = bufferedimage.getHeight();
				//目标将图片裁剪成 宽500，高300
				if (width > 500) {
					/*开始x坐标              开始y坐标             结束x坐标                     结束y坐标*/
					bufferedimage=cropImage(bufferedimage, (width - 500) / 2,0, width - (width-500) / 2, height);
					if (height > 300) {
						bufferedimage=cropImage(bufferedimage,0, (height - 300) / 2,500, height - (height - 300) / 2);
					}
				}else{
					if (height > 300) {
						bufferedimage=cropImage(bufferedimage,0, (height - 300) / 2, width, height - (height - 300) / 2);
					}
				}
				ImageIO.write(bufferedimage, "jpg", f);    //输出裁剪图片
			}
		}
	}


	/**
	 *  裁剪图片，设置原图片的高度 一半
	 * @param oldImg
	 * @param newImg
	 * @param heightSize  高度比例  2 一半
	 */
	public static void resetImgSizeHalf(String oldImg,String newImg, double heightSize){
		try{
			File file = new File(oldImg);
			File file1 = new File(newImg);
			BufferedImage bufferedimage = ImageIO.read(file);
			int width = bufferedimage.getWidth();
			int height = bufferedimage.getHeight();
			bufferedimage = cropImage(bufferedimage,0,0, width,(int) (height / heightSize));
			ImageIO.write(bufferedimage, "jpg", file1);    //输出裁剪图片
		}catch (Exception e){

		}
	}

	/**
	 * 裁剪图片方法
	 * @param Image 图像源
	 * @param startX 裁剪开始x坐标
	 * @param startY 裁剪开始y坐标
	 * @param endX 裁剪结束x坐标
	 * @param endY 裁剪结束y坐标
	 * @return
	 */
	public static BufferedImage cropImage(BufferedImage Image, int startX, int startY, int endX, int endY) {
		int width = Image.getWidth();
		int height = Image.getHeight();
		if (startX == -1) {
			startX = 0;
		}
		if (startY == -1) {
			startY = 0;
		}
		if (endX == -1) {
			endX = width - 1;
		}
		if (endY == -1) {
			endY = height - 1;
		}
		BufferedImage result = new BufferedImage(endX - startX, endY - startY, 4);
		for (int x = startX; x < endX; ++x) {
			for (int y = startY; y < endY; ++y) {
				int rgb = Image.getRGB(x, y);
				result.setRGB(x - startX, y - startY, rgb);
			}
		}
		return result;
	}



	public static void main(String[] args) {

		try {
//			ImageCropUtils.createThumbnail("D:\\32WT320220914173657_0.png", "D:\\32WT320220914173657_01.png","jpg",680 * 2,1024);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

